package uplift.project;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.ContextThemeWrapper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class JoinEventActivity  extends Activity {
	
	private String name;
	private String category;
	private String location;
	private String date;
	private String startTime;
	private String endTime;
	private String description;
	
	private TextView eventName;
	private TextView eventCategory;
	private TextView eventDate;
	private TextView eventLocation;
	private TextView eventTime;
	private TextView eventDescription;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.join_event);
        
        Intent startIntent = this.getIntent();
        Bundle extras = startIntent.getExtras();
        name = extras.getString("name");
        category = extras.getString("category");
        location = extras.getString("location");
        date = extras.getString("date");
        startTime = extras.getString("starts");
        endTime = extras.getString("ends");
        description = extras.getString("description");

        eventName = (TextView) this.findViewById(R.id.textView_previewEventName);
        eventCategory = (TextView) this.findViewById(R.id.textView_previewCategory);
        eventDate = (TextView) this.findViewById(R.id.textView_previewDate);
        eventLocation = (TextView) this.findViewById(R.id.textView_previewLocation);
        eventTime = (TextView) this.findViewById(R.id.textView_previewTime);
        eventDescription = (TextView) this.findViewById(R.id.textView_previewDescription);
        
        eventName.setText(name);
        eventCategory.setText(category);
        eventDate.setText(date);
        eventLocation.setText(location);
        eventTime.setText(startTime + " - " + endTime);
        eventDescription.setText(description);
        eventDescription.setMovementMethod(new ScrollingMovementMethod());
        
		// set the action bar to have up-navigation enabled
	    ActionBar actionBar = getActionBar();
	    actionBar.setDisplayHomeAsUpEnabled(true);
    }
    
    // Asks the user if they really want to unregister from the event.
    public void joinEvent(View v) {
    	alertbox("Joined event", "You successfully joined " + name + ".");
    }
    
    protected void alertbox(String title, String mymessage)
    {
    new AlertDialog.Builder(new ContextThemeWrapper(this, android.R.style.Theme_Holo_Dialog))
       .setMessage(mymessage)
       .setTitle(title)
       .setPositiveButton("OK",
    		   new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int whichButton){
		        	   ApplicationData appState = ((ApplicationData)JoinEventActivity.this.getApplication());
		        	   appState.eventsList.joinEvent(name, category, location, date, startTime, endTime, description);
		           	
			           	// Direct to confirmation page after deleting
			           	Intent i = new Intent(JoinEventActivity.this.getApplicationContext(), UpliftActivity.class);
			           	i.putExtra("name", name);
			           	startActivity(i);
		           }
		       })
       .show();
    }
	// handles action item button presses
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(item.getItemId() == android.R.id.home) {
			finish();
		} else if(item.getItemId() == R.id.menu_home) {
			// go to Home screen
			
			Intent i = new Intent(getApplicationContext(), UpliftActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		} else if(item.getItemId() == R.id.menu_my_events) {
			// go to My Events screen
			
			Intent i = new Intent(getApplicationContext(), MyEventsTabActivity.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
		return true;
	}
	
	// set up Action Bar
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.action_bar, menu);
	    menu.getItem(0);
	    return true;
	}


}